<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater_box">
        <!-- <img src="../assets/logo.png" alt="" /> -->
        <el-avatar :size="100" :src="require('../assets/logo.png')"></el-avatar>
      </div>
      <!-- 登陆表单区域 -->
      <el-form class="login_form demo-ruleForm" :model="login_form" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="login_form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="login_form.password" show-password></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login('loginFormRef')">登陆</el-button>
          <el-button type="info" @click="resetLoginForm('loginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登陆表单数据项
      login_form: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        // 验证用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        // 验证密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm (formName) {
      this.$refs[formName].resetFields()
    },
    login (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.login_form)
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // const token = res.data.token
        // console.log(token)
        // 1.将登陆成功之后的token保存到客户端的sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        //      1.1项目中除了登陆之外的其他API接口，必须在登陆成功之后才能访问
        //      1.2token止应在当前网站打开期间生效，所以将token保存在sessionStorage中
        // 2.通过编程式导航跳转到后台主页，路由地址是/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: aquamarine;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avater_box {
  //   height: 130px;
  //   width: 130px;
  //   border: 1px solid #eee;
  //   border-radius: 50%;
  padding: 10px;
  //   box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  //   background-color: rgb(212, 36, 36);
  //   el-avatar {
  //     width: 100%;
  //     height: 100%;
  //     // border-radius: 50%;
  //   }

}
.btns{
      display: flex;
      justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
